home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / demos / blendCollage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.8 KB  |  253 lines

  1. /* Copyright (c) Silicon Graphics, Inc. 1996 */
  2.  
  3. /* blendCollage.c
  4.  *
  5.  *    Demonstrates how to use the GL_EXT_blend_color extensions
  6.  *    to blend images that do not have an alpha channel.
  7.  *
  8.  *     This program displays images at random positions in a window,
  9.  *    and uses a constant blending function to blend the images.
  10.  *    The blending factor can be changed interactively.
  11.  *    An option allows the images to be zoomed by a randow zoom 
  12.  *    factor. Up to MAX_IMAGES can be read.  To run, type:
  13.  *
  14.  *    blendCollage <imageFile1.rgb> [<imageFile2.rgb>] ...
  15.  *
  16.  *    Try "blendCollage /usr/demos/data/textures/*.rgb"
  17.  *
  18.  *    LEFT Mousebutton, down  - randomly choose a new image 
  19.  *    Up Arrow        - increase blend factor
  20.  *    Down Arrow        - decrease blend factor
  21.  *    <c> key            - clear window to background color
  22.  *    SPACEBAR        - toggle fixed/random zoom
  23.  *    Escape key        - exit the program
  24.  *
  25.  *    David Marsland, MTS, SGI Education R & D, 1993
  26.  */
  27. #include <GL/gl.h>
  28. #include <GL/glu.h>
  29. #include <GL/glut.h>
  30.  
  31. #include <math.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include "rgbImageFile.h"
  35.  
  36. /*  Function Prototypes  */
  37.  
  38. GLvoid  initgfx( GLvoid );
  39. GLvoid  animate( GLvoid );
  40. GLvoid  visibility( int );
  41. GLvoid  drawScene( GLvoid );
  42. GLvoid  reshape( GLsizei, GLsizei );
  43. GLvoid  keyboard( GLubyte, GLint, GLint );
  44. GLvoid  specialkeys( GLint, GLint, GLint );
  45. GLvoid  mouse( GLint, GLint, GLint, GLint );
  46.  
  47. void  printHelp( char * );
  48.  
  49. /* Global Definitions */
  50.  
  51. #define KEY_ESC    27    /* ascii value for the escape key */
  52.  
  53. /* Global Variables */
  54.  
  55. #define     MAX_IMAGES 100
  56.  
  57. typedef struct _image {
  58.     GLuint    *image;
  59.     int    width, height;
  60. } ImageInfo;
  61.  
  62. static ImageInfo    images[MAX_IMAGES];
  63. static ImageInfo    *curImage = NULL;
  64. static GLint        imageCount;
  65. static GLint        xPos, yPos;
  66.  
  67. static GLsizei        winWidth, winHeight;
  68. static GLsizei        screenWidth, screenHeight;
  69.  
  70. static GLboolean    zoomFlag = GL_FALSE;
  71.  
  72. static GLfloat        blend_factor = 0.5;
  73.  
  74. GLvoid
  75. main ( int argc, char *argv[] )
  76. {
  77.     int    i;
  78.  
  79.     glutInit( &argc, argv );
  80.  
  81.     if (argc < 2) {
  82.         fprintf(stderr, 
  83.               "usage: %s <imageFile1> [<imageFile2> ... ]\n", argv[0] );
  84.         exit (1);
  85.     }
  86.  
  87.     imageCount = argc - 1;
  88.     if (imageCount > MAX_IMAGES) imageCount = MAX_IMAGES;
  89.  
  90.     srand48( imageCount );    /* seed random number generator */
  91.  
  92.     /* read in all image files specifed as command line args */
  93.     for ( i = 0; i < imageCount; i++ ) {
  94.         images[i].image = rgbReadImageFile( argv[i+1],
  95.                     &images[i].width, &images[i].height );
  96.     }
  97.     curImage = &images[0];
  98.  
  99.     /* create a window that is 3/4 the size of the screen */
  100.  
  101.     screenWidth = glutGet( GLUT_SCREEN_WIDTH ); 
  102.     screenHeight = glutGet( GLUT_SCREEN_HEIGHT );
  103.     winWidth = 3*screenWidth/4;
  104.     winHeight = 3*screenHeight/4;
  105.     glutInitWindowPosition( winWidth / 8, winHeight / 8 );
  106.     glutInitWindowSize( winWidth, winHeight );
  107.     glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
  108.     glutCreateWindow( argv[0] );
  109.  
  110.     initgfx();
  111.  
  112.     glutMouseFunc( mouse );
  113.     glutKeyboardFunc( keyboard );
  114.     glutSpecialFunc( specialkeys );
  115.     glutReshapeFunc( reshape );
  116.     glutIdleFunc( animate ); 
  117.     glutVisibilityFunc( visibility ); 
  118.     glutDisplayFunc( drawScene ); 
  119.  
  120.     printHelp( argv[0] );
  121.  
  122.     glutMainLoop();
  123. }
  124.  
  125. GLvoid
  126. printHelp( char *progname )
  127. {
  128.     fprintf(stdout, "\n\n%s - creates a collage of images "
  129.         "which can be zoomed\n\n"
  130.         "UP Arrow         - increase blend factor\n" 
  131.         "DOWN Arrow         - decrease blend factor\n"
  132.         "<c> Key        - clear the display\n"
  133.         "SPACEBAR        - toggle fixed/random zoom\n"
  134.         "Escape key        - exit the program\n\n",
  135.         progname);
  136.     fprintf(stdout, "blend_factor = %4.2f\n", blend_factor );
  137. }
  138.  
  139. void
  140. initgfx( void )
  141. {
  142.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  143.     glClear( GL_COLOR_BUFFER_BIT );
  144.  
  145.     glEnable( GL_BLEND );
  146.     glBlendColorEXT( 0, 0, 0, blend_factor );
  147.     glBlendFunc( GL_CONSTANT_ALPHA_EXT, GL_ONE_MINUS_CONSTANT_ALPHA_EXT );
  148. }
  149.  
  150. GLvoid 
  151. keyboard( GLubyte key, GLint x, GLint y )
  152. {
  153.     switch (key) {
  154.     case ' ':
  155.         zoomFlag = !zoomFlag;
  156.         if ( !zoomFlag )
  157.             glPixelZoom( 1.0, 1.0 );
  158.         glutPostRedisplay();
  159.         break;
  160.     case 'c':
  161.         glClear( GL_COLOR_BUFFER_BIT );
  162.         glutPostRedisplay();
  163.         break;
  164.         
  165.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  166.         exit(0);
  167.     }
  168. }
  169.  
  170. GLvoid 
  171. specialkeys( GLint key, GLint x, GLint y )
  172. {
  173.     switch (key) {
  174.     case GLUT_KEY_UP:    /* increase blend factor */
  175.         if (blend_factor < 1.0) blend_factor += 0.1;
  176.         break;
  177.     case GLUT_KEY_DOWN:    /* decrease blend factor */
  178.         if (blend_factor > 0.0) blend_factor -= 0.1;
  179.         break;
  180.     }
  181.     printf( "blend_factor = %4.2f\n", blend_factor );
  182.     glBlendColorEXT( 0, 0, 0, blend_factor );
  183.     glutPostRedisplay();
  184. }
  185.  
  186. GLvoid
  187. mouse( GLint button, GLint state, GLint x, GLint y )
  188. {
  189.     if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  190.         /* grab the new mouse location */
  191.         xPos = x;
  192.         yPos = winHeight - y;
  193.  
  194.         /* pick a new image index */
  195.  
  196.         curImage = &images[(int) (imageCount * drand48())];
  197.     }
  198. }
  199.  
  200. GLvoid
  201. reshape( GLsizei width, GLsizei height )
  202. {
  203.     glViewport( 0, 0, width - 1, height - 1);
  204.  
  205.     winWidth = width;
  206.     winHeight = height;
  207.  
  208.     glMatrixMode( GL_PROJECTION );
  209.     glLoadIdentity();
  210.     gluOrtho2D( 0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight );
  211.     glMatrixMode( GL_MODELVIEW );
  212.     glLoadIdentity();
  213.     glTranslatef( 0.375, 0.375, 0.0 );
  214.     glClear( GL_COLOR_BUFFER_BIT );
  215. }
  216.  
  217. GLvoid
  218. animate( GLvoid )
  219. {
  220.     /* if zooming is enabled, randomly change the zoom factor */
  221.     if ( zoomFlag ) {
  222.         /* generates zoom values in the range [ -2.0, 2.0 ) */
  223.         glPixelZoom( ( drand48() * 4.0 ) - 2.0, 
  224.                 ( drand48() * 4.0 ) - 2.0 );
  225.     }
  226.  
  227.         /* Tell GLUT to redraw the scene */
  228.         glutPostRedisplay();
  229. }
  230.         
  231. GLvoid
  232. visibility( int state )
  233. {
  234.         if (state == GLUT_VISIBLE) {
  235.                 glutIdleFunc( animate );
  236.         } else {
  237.                 glutIdleFunc( NULL );
  238.         }
  239. }
  240.  
  241. GLvoid
  242. drawScene( GLvoid )
  243. {
  244.     xPos += (2.0*drand48() - 1.0)*.05*screenWidth;
  245.     yPos += (2.0*drand48() - 1.0)*.05*screenHeight;
  246.     xPos = fabs( xPos );
  247.     yPos = fabs( yPos );
  248.     glRasterPos2i( xPos, yPos );
  249.     glDrawPixels( curImage->width, curImage->height,
  250.             GL_RGBA, GL_UNSIGNED_BYTE, curImage->image );
  251.     glFlush();
  252. }
  253.